package com.hyst.web;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Recommend;
import com.hyst.domain.StudentView;
import com.hyst.service.RecommendService;

/**
 * @Description:controller
 * @author：dy 
 *@Date：2018年09月26日
 */
@RestController
@RequestMapping("recommends")
@CrossOrigin(maxAge = 3600, origins = "*")
public class RecommendController {
	@Autowired
	private RecommendService recommendService;
	
	@GetMapping
	public ResultPage<Recommend> list(Recommend recommend) {
		return ResultPage.success(recommendService.list(recommend));
	}

	@PostMapping
	public ResultBean<Recommend> creat(final Recommend recommend) {
		return ResultBean.success(recommendService.insert(recommend));
	}

	/**
	 * @Description: 根据主键查询
	 * @param studentId
	 * @return ResultBean<Recommend>
	 */
	@GetMapping("{studentId}")
	public ResultBean<Recommend> getByPriKey(@PathVariable("studentId") final String studentId) {
		return ResultBean.success(recommendService.getByPriKey(studentId));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param recommend
	 * @return ResultBean<Recommend>
	 */
	@PatchMapping("{studentId}")
	public ResultBean<Recommend> update(final Recommend recommend) {
		return ResultBean.success(recommendService.update(recommend));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param recommend
	 * @return ResultBean<Recommend>
	 */
	//@PutMapping("{studentId}")
	@PostMapping("{studentId}")
	public ResultBean<Recommend> updatePatch(@PathVariable("studentId") final String studentId,
			final Recommend recommend) {
		return ResultBean.success(recommendService.updateSelective(recommend));
	}

	/**
	 * @Description: 根据主键删除
	 * @param studentId
	 * @return ResultBean<Recommend>
	 */
	@DeleteMapping("{studentId}")
	public ResultBean<Recommend> delete(@PathVariable("studentId") final String studentId) {
		return ResultBean.success(recommendService.delete(studentId));
	}
	
	// 条件查询
	@GetMapping("selectByFuzeren/{fuzerenId}")
	public ResultPage<Recommend> getByFuzerenId(final Recommend recommend,@PathVariable("fuzerenId") final String fuzerenId) {
//		String fuzerenIdtest = null;
//		try {
//			fuzerenIdtest =new String(fuzerenId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		System.out.println(fuzerenId);
		return ResultPage.success(recommendService.getDataByFuzerenId(recommend,fuzerenId));
	}


}
